//
//  BEHomeController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/7/26.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit
import SnapKit
import SDCycleScrollView


class BEHomeController: BEBaseViewController,SDCycleScrollViewDelegate {
    
    var topScrollView: SDCycleScrollView!
    
    var imagesURLStrings: NSArray = {
        let imagesURLStrings = [
            "http://www.primotorc.cn/pic/20141216032656888.jpg",
            "http://image90.360doc.com/DownloadImg/2015/11/0412/61088051_39.jpg",
            "http://pic4.zhongsou.com/img?id=522541528752cb79a3d",
            "http://pic41.nipic.com/20140502/6608733_130656271000_2.jpg",
            "http://picapi.ooopic.com/10/68/43/48b1OOOPIC4a.jpg"]
        return imagesURLStrings as NSArray
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = COLOR_ALL_BG
        
        setupUI()
    }
    
    private func setupUI(){
//        let titImgView = UIImageView(image: UIImage(named: "Global-Oiler"))
//        navigationItem.titleView = titImgView
//        navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: 62, height: 24)
        
        automaticallyAdjustsScrollViewInsets = false
        topScrollView = SDCycleScrollView(frame: CGRect.zero, delegate: self, placeholderImage: UIImage(named: "button"))
        
        // 网络加载图片的轮播器
        topScrollView.imageURLStringsGroup = imagesURLStrings as [AnyObject];
        view.addSubview(topScrollView)
        
        let hireBtn = createBtn(title: "My Orders", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "my-orders", target: self, action: #selector(hireBtnClick))
        
        let techsBtn = createBtn(title: "My Schedules", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "my-schedules", target: self, action: #selector(techsBtnClick))
        
        let advantBtn = createBtn(title: "My Account", normalColor: UIColor.white, highlightedColor: UIColor.white, fontSize: 20, backgroundImageName: "my-account", target: self, action: #selector(advantBtnClick))
        
        view.addSubview(hireBtn)
        view.addSubview(techsBtn)
        view.addSubview(advantBtn)
        
        hireBtn.layer.cornerRadius = 5
        hireBtn.layer.masksToBounds = true
        techsBtn.layer.cornerRadius = 5
        techsBtn.layer.masksToBounds = true
        advantBtn.layer.cornerRadius = 5
        advantBtn.layer.masksToBounds = true
        
        topScrollView.snp.makeConstraints { (make) in
            make.width.equalTo(SCREEN_WIDTH)
            make.top.equalTo(64)
            make.height.equalTo(200)
        }
        
        hireBtn.snp.makeConstraints { (make) in
            make.top.equalTo(topScrollView.snp.bottom).offset(5)
            make.left.equalTo(5)
            make.right.equalTo(-5)
        }
  
        techsBtn.snp.makeConstraints { (make) in
            make.top.equalTo(hireBtn.snp.bottom).offset(5)
            make.left.equalTo(5)
            make.bottom.equalTo(-53)
            make.height.equalTo(hireBtn.height*0.68)
        }
        
        advantBtn.snp.makeConstraints { (make) in
            make.top.equalTo(hireBtn.snp.bottom).offset(5)
            make.right.equalTo(-5)
            make.bottom.equalTo(-53)
            make.height.width.equalTo(techsBtn)
            make.left.equalTo(techsBtn.snp.right).offset(5)
        }
    }
    
    
    //便利的创建btn的方法
    func createBtn(title:String?,normalColor:UIColor,highlightedColor:UIColor,fontSize:CGFloat,backgroundImageName:String?,target: AnyObject?, action: Selector) -> UIButton{
        
        let button = UIButton();
        
        button.setTitle(title, for: .normal)
        button.setTitleColor(normalColor, for: .normal)
        button.setTitleColor(highlightedColor, for: .highlighted)
        
        button.titleLabel!.font = baseFont(size: fontSize)
        
        button.addTarget(target, action: action, for: .touchUpInside)
        
        if let str = backgroundImageName {
            button.setBackgroundImage(UIImage(named:str), for: .normal)
            let hightStr = str.appending("_highlighted")
            button.setBackgroundImage(UIImage(named:hightStr), for: .highlighted)
        }
        button.adjustsImageWhenHighlighted = false
        button.sizeToFit()
        return button
    }
    
    
    @objc private func hireBtnClick(){
        if !BEAccountModel.isLogin(){
            let alertVC = UIAlertController(title: "Please login first!", message: "Log in before you can view the orders", preferredStyle: .alert)
            
            let cancleAction = UIAlertAction(title: "Later", style: .cancel) { (_) in

            }
            
            let doneAction = UIAlertAction(title: "Login Now", style: .default) { (_) in
                _ = isLogin()
            }
            alertVC.addAction(cancleAction)
            alertVC.addAction(doneAction)
            
            UIApplication.shared.keyWindow?.rootViewController!.present(alertVC, animated: true, completion: nil)
            return
        }
        tabBarController?.selectedIndex = 1
    }
    
    
    @objc private func techsBtnClick(){

    }
    
    @objc private func advantBtnClick(){
    }
}
